package com.leyou.auth.job;

import com.leyou.auth.service.AliAuthService;
import com.leyou.common.redis.RedisConstant;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Set;

@Component
public class ClearImageJob {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private AliAuthService aliAuthService;

    @Scheduled(cron = "* * 23 * * ?") //  20秒 直接在方法上使用注解即可完成扫描
    public void run() {
        System.out.println("----执行扫描一次1---");
        Set<String> difference = redisTemplate.opsForSet().difference(RedisConstant.BRAND_PIC_RESOURCES, RedisConstant.BRAND_PIC_DB_RESOURCES);

        if (!CollectionUtils.isEmpty(difference)) {
            difference.forEach(image -> {
                System.out.println("image = " + image.toString());
                String[] split = image.split("com/");
                if (split.length >1)
                aliAuthService.deleteFile(split[1]);
            });
        }
        redisTemplate.delete(RedisConstant.BRAND_PIC_DB_RESOURCES);
        redisTemplate.delete(RedisConstant.BRAND_PIC_RESOURCES);
    }
}
